<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;
use think\Db;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\response\Json;

/**
 * 提现审核
 *
 * @icon fa fa-circle-o
 */
class CashAuditing extends Backend
{

    /**
     * Bill模型对象
     * @var \app\admin\model\user\Bill
     */
    protected $model = null;

    protected $relationSearch = true;
    protected $searchFields = 'u.name,u.nickname,u.mobile';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\user\Bill;

    }


    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->field("user_bill.*")
            ->field("u.name,u.nickname,u.mobile")
            ->join('user u','u.id = user_bill.user_id')
            ->where("source",0)
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);

        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }


    //审核 通过
    public function shenhe()
    {

        $id = input("id");
        if (empty($id)){
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $bill = Db::name("user_bill")->where("id",$id)->find();

        if ($bill["audit_status"] == 1) {
            $this->error("无需重复审核");
        }

        $money =$bill["money"];
        if ($money < 0) {
            $money = $money * -1;
        }

        $user = Db::name("user")->field("id,openid")->where("id",$bill["user_id"])->find();

        $request_params = [
            'amount'=>intval($money * 100),
            'trans_no'=>$bill["order_no"],
            'desc'=>"佣金提现",
            'openid'=>$user["openid"],
        ];
        $tixian = new \wxpay\Tixian;
        $res = $tixian->WechatTransfer($request_params);
        halt($res);

        Db::name("user_bill")->where("id",$id)->update([
            "audit_time"=>time(),
            "audit_status"=>1,
            "rejection_info"=>""
        ]);
        $this->success("审核成功");
    }


    //审核 驳回
    public function bohui()
    {

        $id = input("id");
        $info = input("rejection_info");
        if (empty($id)&&empty($info)){
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $user_money = Db::name("user_bill")->where("id",$id)->find();

        if (empty($user_money)) {
            $this->error("记录不存在");
        }

        Db::startTrans();
        try {
            $money = $user_money["money"];
            if ($money < 0) {
                $money = $money * -1;
            }
            $user = Db::name("user")->where("id",$user_money["user_id"])->inc("money",$money)->update();
            if (!$user) {
                $this->error("驳回失败");
            }



            $result =Db::name("user_bill")->where("id",$id)->update([
                "audit_time"=>time(),
                "audit_status"=>2,
                "rejection_info"=>$info
            ]);
            if ($result) {
                Db::commit();
                $this->success("已驳回");
            }
            Db::rollback();
        }catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error("服务超时请稍后再试");
        }



    }


    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


}
